<template>
  <div class='m-firmDepartment'>
    <div style='padding-bottom: 20px;'>
      <el-button type='primary' @click='addDepartment'>添加部门</el-button>
    </div>
    <a-table :data='data' :cols='tableKeys' key='t1'>
      <el-table-column align="center" label="操作" width='240' slot='ahead'>
        <template slot-scope="scope">
          <div class='m-firmDepartment-table-handle'>
            <el-button size='mini' @click='updateDepartment(scope.row, "cancel")'>修改</el-button>
            <el-button size='mini' @click='updateEmployees(scope.row.id, "finish")'>修改员工</el-button>
            <el-button size='mini' @click='deleteDepartment(scope.row.id)'>删除</el-button>
          </div>
        </template>
      </el-table-column>
      <el-table-column align="center" label="下属员工" min-width="250">
        <template slot-scope="scope">
          <div>{{ scope.row.staff_names || '暂无' }}</div>
        </template>
      </el-table-column>
    </a-table>
    <div class='u-pagination'>
      <!-- 分页 -->
      <pagination ref='page' @change='getData' :action='fetchData' :pageSize='20'></pagination>
    </div>

    <employess-configure ref='dialog' 
      getStaffsPath='firmGetDeptstaffs'
      getExStaffsPath='firmGetExternalStaffs'
      addStaffPath='firmAddDeptStaff'
      deleteStaffPath='firmDeleteDeptStaff'
      :id='selectId'
      idKey='aDeptId'
      @close='closeDialog'>
    </employess-configure>
  </div>
</template>

<script>
import employessConfigure from '@/components/employessConfigure'

export default {
  name: 'firmDepartment',

  components: {
    employessConfigure
  },

  data() {
    return {
      tableKeys: [
        { prop: 'name', label: '部门名称', minWidth: '150' }
      ],
      data: [],
      ifRequesting: false,
      selectId: ''
    }
  },

  mounted() {
    this.$refs.page.changePage()
  },

  methods: {
    async fetchData(aPageNum, aPageSize) {
      return this.$api.firmGetDepartments({
        aPageNum,
        aPageSize,
        aFirmId: this.aFirmId
      })
    },

    getData(data) {
      this.data = data
    },

    async addDepartmentRequest(name) {
      const { code } = await this.$api.firmCreateDepartments({
        aFirmId: this.aFirmId,
        aDeptName: name
      })
      return code === 0
    },

    addDepartment() {
      if (this.ifRequesting) return false
      this.$prompt('请输入部门名称', '添加部门', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        inputPattern: /^.{1,10}$/,
        inputErrorMessage: '请输入内容'
      }).then(async ({ value }) => {
        this.ifRequesting = true
        const result = await this.addDepartmentRequest(value)
        this.ifRequesting = false
        if (!result) return false
        this.$refs.page.changePage()
        this.$message({
          type: 'success',
          message: '添加的部门是：' + value
        })
      }).catch(() => {
        this.$message({
          type: 'info',
          message: '取消输入'
        })
      })
    },

    async updateDepartmentRequest(name, aDeptId) {
      const { code } = await this.$api.firmUpdateDepartments({
        aDeptId,
        aDeptName: name
      })
      return code === 0
    },

    updateDepartment(v) {
      if (this.ifRequesting) return false
      this.$prompt('请输入部门名称', '修改部门名称', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        inputPattern: /^.{1,10}$/,
        inputErrorMessage: '请输入内容',
        inputValue: v.name
      }).then(async ({ value }) => {
        this.ifRequesting = true
        const result = await this.updateDepartmentRequest(value, v.id)
        this.ifRequesting = false
        if (!result) return false
        this.$refs.page.changePage()
        this.$message({
          type: 'success',
          message: '修改部门名称为：' + value
        })
      }).catch(() => {
        this.$message({
          type: 'info',
          message: '取消修改'
        })
      })
    },

    deleteDepartment(aDeptId) {
      if (this.ifRequesting) return false
      this.__messageBox({
        title: '你确认要删除该部门吗？',
        successCallback: async () => {
          this.ifRequesting = true
          const { code } = await this.$api.firmDeleteDepartments({ aDeptId })
          this.ifRequesting = false
          this.$refs.page.changePage()
          return code
        }
      })
    },

    getString(array) {
      return array.join(',')
    },

    updateEmployees(id) {
      this.selectId = id
      this.$refs.dialog.show()
    },

    closeDialog() {
      this.$refs.page.changePage()
    }
  }
}
</script>

<style lang='less'>
.m-firmDepartment {
}
</style>